(import
 (fslib)
 (prefix (commands cut) cmd:)
 (shell-state)
 ;; receive form
 (srfi srfi-8)
 (srfi srfi-64))

;; Example from the docs:
;; Initialize and give a name to a simple testsuite.
(test-begin "cut-test")
(test-group "cut-test"
  (define main-directory (getcwd))
  (define test-directory (fsing-join main-directory "test"))
  (define integration-directory
    (fsing-join test-directory "integration"))
  (define default-test-shell-state
    (update-shell-state default-shell-state
                        'current-working-directory
                        integration-directory))

  (test-equal "cut - read from file - range of fields - delimiter '/'"
    (list "b/c"
          "3/4-5")
    (let ([initial-shell-state default-test-shell-state]
          [initial-dir integration-directory])
      ;; setup for integration tests
      (chdir initial-dir)
      (let ([input-filename (fsing-join "generated" "cut-input.txt")])
        (with-output-to-file input-filename
          (λ ()
            (simple-format #t "~a\n" "a/b/c/d/e")
            (simple-format #t "~a\n" "1-2/3/4-5")))
        (receive (result state) (cmd:cut '(2 . 3)
                                         input-filename
                                         #:delimiter #\/
                                         #:shell-state initial-shell-state)
          result))))

  (test-equal "cut - read from file - range of fields - delimiter `-'"
    (list "d-e"
          "2/3/4-5")
    (let ([initial-shell-state default-test-shell-state]
          [initial-dir integration-directory])
      ;; setup for integration tests
      (chdir initial-dir)
      (let ([input-filename (fsing-join "generated" "cut-input.txt")])
        (with-output-to-file input-filename
          (λ ()
            (simple-format #t "~a\n" "a/b/c-d-e")
            (simple-format #t "~a\n" "1-2/3/4-5")))
        (receive (result state) (cmd:cut '(2 . 3)
                                         input-filename
                                         #:delimiter #\-
                                         #:shell-state initial-shell-state)
          result))))

  (test-equal "cut - read from file - range of fields - delimiter `-' - output delimiter `::'"
    (list "d::e"
          "2/3/4::5")
    (let ([initial-shell-state default-test-shell-state]
          [initial-dir integration-directory])
      ;; setup for integration tests
      (chdir initial-dir)
      (let ([input-filename (fsing-join "generated" "cut-input.txt")])
        (with-output-to-file input-filename
          (λ ()
            (simple-format #t "~a\n" "a/b/c-d-e")
            (simple-format #t "~a\n" "1-2/3/4-5")))
        (receive (result state) (cmd:cut '(2 . 3)
                                         input-filename
                                         #:delimiter #\-
                                         #:output-delimiter "::"
                                         #:shell-state initial-shell-state)
          result))))

  (test-equal "cut - read from input-port - list of fields - delimiter `-'"
    (list "a/b/c-e-f"
          "1-5-6")
    (let ([initial-shell-state default-test-shell-state])
      (with-input-from-string "a/b/c-d-e-f\n1-2/3/4-5-6"
        (λ ()
          (receive (result state) (cmd:cut '(1 3 4)
                                           #:delimiter #\-
                                           #:shell-state initial-shell-state)
            result)))))

  (test-equal "cut - use previous result - list of fields - delimiter `-'"
    (list "a/b/c-e-f"
          "1-5-6")
    (let ([initial-dir integration-directory])
      (receive (result _state)
          (cmd:cut '(1 3 4)
                   #:delimiter #\-
                   #:previous-result '("a/b/c-d-e-f"
                                       "1-2/3/4-5-6"))
        result))))

;; Finish the testsuite, and report results.
(test-end "cut-test")


#;(with-input-from-string "my,line,parts\nyour,line,parts\n"
(λ ()
(cut '(1 3) #:delimiter #\,)))

;; EXAMPLE CALLS

;; (cut '(1 . 3)
;;      #:delimiter #\/)  ; --> read from input port
;; (cut '(1 2 3) (receive (result _) (pwd) (car result))  #:delimiter #\/)
;; (cut '(1 2) (receive (result _) (pwd) (car result))  #:delimiter #\/)
;; (cut 2 (receive (result _) (pwd) (car result)) #:delimiter #\/)
;; TODO: use previous result
;; TODO: idea: allow list of delimiters
